<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$this->model('model')->check_custom();
$this->model('model')->check_worker('',true);
$return['content_types']=content_types();
if($_RQ['op']=='main'){
	if($_RQ['type']){
		$_SESSION['content_type']=$_RQ['type'];
	}
	$_SESSION['content_type']=$_SESSION['content_type']?:'text';
	$return['params']=array('type'=>$_SESSION['content_type']?:'text');	
	$return['params']['page']=$pindex;
	$return['params']['group_id']=-2;
	$return['groups']=array();
	foreach($return['content_types'] as $k=>&$gv){
		$return['groups'][$k]=pdo_getall('core_account_content_group',array('type'=>$k,'uniacid'=>$_SESSION['uniacid']));
		$nogroup=array('id'=>0,'title'=>'未分组');
		array_unshift($return['groups'][$k],$nogroup);
		$all=array('id'=>-2,'title'=>'全部');
		array_unshift($return['groups'][$k],$all);
	}
	set_html('title','内容云');
	exi($return);
}
if($_RQ['op']=='list'){
	if($_RQ['type']){
		$_SESSION['content_type']=$_RQ['type'];
	}
	$_SESSION['content_type']=$_SESSION['content_type']?:'text';
	$return['params']=array('type'=>$_SESSION['content_type']);	
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>$_SESSION['content_type']);
	if(isset($_RQ['group_id']) && $_RQ['group_id']!='' && $_RQ['group_id']!=-2){
		$params['group_id']=intval($_RQ['group_id']);
		$return['params']['group_id']=$_RQ['group_id'];
	}else{
		$return['params']['group_id']=-2;
	}
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 24;
	$total='';
	$list=pdo_getslice('core_account_content',$params,array($pindex,$psize),$total,'','',$orderby);
	foreach($list as &$item){
		$item['content']=cfc('tools')->substr($item['content'],0,108);
		$item['h5url']=custom_url($item['h5url'],true);
		if($item['type']=='image' || $item['type']=='voice' || $item['type']=='video'  || $item['type']=='file'){
			if(!$item['appmedia_id'] || $item['appmedia_uptime']<TIMESTAMP-3*24*3600-200){
				$item['appmedia_id']=model('core/wxwork_userapi')->upload_media($item['image'],$item['type']);
				$update=array(
					'appmedia_id'=>$item['appmedia_id'],
					'appmedia_uptime'=>TIMESTAMP,
				);
				if($item['appmedia_id']){
					pdo_update('core_account_content',$update,array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
				}
			}
		}
		$item['image']=tomedia($item['image']);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page']=$pindex;
	set_html('title','内容云');
	exi($return);
}
if($_RQ['op']=='wechat_image_url'){
	//将选中的图片地址转换为微信图片地址
	$list=str_array($_RQ['imagelist']);
	foreach($list as &$image){
		$image=cfc('files')->wechat_image_url($image);
	}
	exi($list);
}